﻿using RazorEngine.Configuration;
using RazorEngine.Templating;
using RazorEngine.Text;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XjjXmm.CodeGenerator.FileStangy;

namespace XjjXmm.CodeGenerator;

internal class CodeGenerator
{
    private readonly List<TableInfo> _tables;
    private readonly IRazorEngineService _service;

    public CodeGenerator()
    {
        var config = new TemplateServiceConfiguration();
        config.Debug = true;
        config.EncodedStringFactory = new RawStringFactory();
        _service = RazorEngineService.Create(config);
    }
    public CodeGenerator(string connectionString, DbType dbType)
    {
        //var connectionString = "Server=192.168.88.25; Port=3306; Database=admindb; Uid=root; Pwd=123456; Charset=utf8mb4;";
        //var dbType = DbType.MySql;

        var dbContext = new DbModel(connectionString, dbType);
        _tables = dbContext.TableInfos;

        //foreach (var table in tables)
        //{
        //    Console.WriteLine($"{ table.Name}:{table.Description}");

        //    var colunms = dbContext.GetColumnInfosByTableName(table.Name);
        //    foreach (var column in colunms)
        //    {
        //        //Console.WriteLine($"{JsonConvert.SerializeObject(column)}");

        //        Console.WriteLine($"{column.DbColumnName}:{column.DataType}:{column.ColumnDescription}:{column.IsPrimarykey}:{column.IsIdentity}:{column.IsNullable}");
        //    }

        //    break;
        //}

        var config = new TemplateServiceConfiguration();
        config.Debug = true;
        config.EncodedStringFactory = new RawStringFactory();
        _service = RazorEngineService.Create(config);
    }

    public void Generator(string templateName, IGeneratorStrangy strangy, string tableName = "")
    {
        var baseDir = AppDomain.CurrentDomain.BaseDirectory;
        string templateDir = Path.Combine(baseDir, "template", templateName);
        var fileInfo = new FileInfo(templateDir);

        var tables = _tables.Where(t =>
        {
            if (string.IsNullOrEmpty(tableName))
            {
                return true;
            }
            return t.DbName == tableName;
            }
        );

        var task = new    List<Task>();
        foreach (var table in tables)
        {
            var tmp = strangy.GetTableModel(table);

            var result = _service.RunCompile(File.ReadAllText(templateDir), fileInfo.Name, null, (object)tmp);

           task.Add( File.WriteAllTextAsync($"template/code/{tmp.PropertyName}.cs", result));
        }

        task.ForEach(t => t.Wait());
        
        //var files = Directory.GetFiles(templateDir, "*", SearchOption.AllDirectories);
        //Dictionary<string, string> templist = new Dictionary<string, string>();
        //foreach (var item in files)
        //{
        //    templist.Add(Path.GetFileNameWithoutExtension(item), File.ReadAllText(item));
        //}
        //foreach (var item in templist)
        //{
        //    var result = _service.RunCompile(item.Value, item.Key, null, _tables.FirstOrDefault());
        //    var a = "";
        //}
    }

    public void GeneratorEntity(string templateName, IFileGeneratorStrangy strangy)
    {

        var baseDir = AppDomain.CurrentDomain.BaseDirectory;

        var files = Directory.GetFiles(Path.Combine(baseDir, "source"));

        string templateDir = Path.Combine(baseDir, "template", templateName);
        var fileInfo = new FileInfo(templateDir);

        foreach (var file in files)
        {
            var tmp = strangy.Generator(file);

            var result = _service.RunCompile(File.ReadAllText(templateDir), fileInfo.Name, null, (object)tmp);

            File.WriteAllTextAsync($"template/code/{tmp.clazz}ConvertService.java", result);
        }
        //foreach (var table in _tables)
        //{
        //    foreach(var colunm in table.ColumnInfos)
        //    {
        //        colunm.DataType
        //    }
        //}

    }




}
